import Taro from '@tarojs/taro';
import request from '../service/api';

export default {
  namespace: 'order',
  state: {
    recordList: [],
    current: 0,
    pageNum: 1,
    total: 0,
    fleetRecordList: [],
    bookOrderList: [],
    fleetPageNum: 1,
    fleetTotal: 0,
    preRecord: {}
  },
  effects: {
    * init(_, {call, put}) {
      const {data: {code, data: {records = [], total} = {}} = {}} = yield call(() =>
        request.post('/mini/order/findByPageUserAllOrder', {pageNum: 1, pageSize: 20})); //9999
      if (code && code === 200) {
        yield put({
          type: 'setState',
          payload: {
            recordList: records,
            total
          }
        })
      }
    },
    * loadMore({payload}, {call, put, select}) {
      const {recordList, pageNum} = yield select(({order}) => order)
      const {data: {code, data: {records = []} = {}} = {}} = yield call(() =>
        request.post('/mini/order/findByPageUserAllOrder', {pageNum: pageNum + 1, pageSize: 20})); //9999
      if (code && code === 200) {
        yield put({
          type: 'setState',
          payload: {
            recordList: recordList.concat(records),
            pageNum: pageNum + 1
          }
        })
      }
    },
    * initFleetPage({payload}, {call, put}) {
      const {fleetAccountId} = payload;
      const {data: {code, data: {records = [], total} = {}} = {}} = yield call(() =>
        request.get('/mini/other/fleetFlowById', {fleetId: fleetAccountId, pageNum: 1, pageSize: 20})); //9999
      if (code && code === 200) {
        yield put({
          type: 'setState',
          payload: {
            fleetRecordList: records,
            fleetTotal: total,
            fleetId: fleetAccountId
          }
        })
      }
    },
    * loadMoreFleetOrder({payload}, {call, put, select}) {
      const {fleetRecordList, fleetPageNum, fleetId} = yield select(({order}) => order)
      const {data: {code, data: {records = []} = {}} = {}} = yield call(() =>
        request.get('/mini/other/fleetFlowById', {fleetId, pageNum: fleetPageNum + 1, pageSize: 20})); //9999
      if (code && code === 200) {
        yield put({
          type: 'setState',
          payload: {
            fleetRecordList: fleetRecordList.concat(records),
            fleetPageNum: fleetPageNum + 1
          }
        })
      }
    },
    // 预约充电
    * addOrder({payload}, {call, put, select}){
      console.log('addOrder', payload);
      console.log('payload?.scheduledChargingTime.toString()', payload?.scheduledChargingTime.toString());
      console.log('new Date(payload?.scheduledChargingTime)', new Date(payload?.scheduledChargingTime));
      const scheduledChargingTime = payload?.scheduledChargingTime.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
      console.log('scheduledChargingTime', scheduledChargingTime);
      const {data: {code, msg = ''} = {}} = yield call(() => request.post('/scheduleChargingRecord/add', {
        ...payload,
      }));
      if (code && code === 200){
        Taro.showToast({
          title: '预约成功',
          icon: 'success'
        });
        Taro.redirectTo({
          url: '/moduleA/pages/order/bookOrder'
        })
      } else {
        Taro.showToast({
          title: msg,
          icon: 'none'
        })
      }
    },
    *loadBookOrder(_, {call, put}){
      const {data: {code, data = []} = {}} = yield call(() => request.get('/scheduleChargingRecord/list'));
      console.log('loadBookOrder', data);
      yield put({
        type: 'setState',
        payload: {
          bookOrderList: data
        }
      })
    },
    *deleteBookOrder({payload}, {call, put}){
      const {data: {code, msg}} = yield call(() => request.get(`/scheduleChargingRecord/cancel?id=${payload.id}`));
      // console.log('取消预约回调', data);
      if(code && code === 200){
        Taro.showToast({
          title: '预约已成功取消',
          icon: 'none'
        });
        yield put({
          type: 'order/loadBookOrder'
        })
      } else {
        Taro.showToast({
          title: '操作失败,请重试',
          icon: 'none'
        })
      }
    },
    *getPreRecord(_, {call, put}){
      const {data: {code, data = {}} = {}} = yield call(() => request.get('/scheduleChargingInfo/getOne'));
      if (code && code === 200){
        yield put({
          type: 'setState',
          payload: {
            preRecord: data
          }
        })
      }
    }
  },
  reducers: {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    }
  }
}
